package com.push.example;

import java.io.IOException;
import java.util.List;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.push.example.model.DeviceSecureStorage;

@SuppressWarnings("serial")
public class DeleteAll extends HttpServlet {

	public static final PersistenceManagerFactory pmfInstance = JDOHelper
			.getPersistenceManagerFactory("transactions-optional");

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
			try {
				PersistenceManager pm = Register.pmfInstance.getPersistenceManager();
				String query = "select from " + DeviceSecureStorage.class.getName();
				List<DeviceSecureStorage> devices = (List<DeviceSecureStorage>) pm
						.newQuery(query).execute();
				pm.deletePersistentAll(devices);

			} catch (Exception e) {
				resp.getWriter().println(
						"<result error='true'>" + e.toString() + "</result>");
			}
	}
}
